// widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpSocket>
#include <QString>
#include <QListWidget>
#include <QCamera>
#include <QMediaCaptureSession>
#include <QVideoSink>
#include <QUdpSocket>
#include <QNetworkDatagram>
#include <QImage>
#include <QVideoFrame>
#include <QAudioFormat>
#include <QAudioSource>
#include <QMediaDevices>
#include <QAudioSink>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void setTcpSocket(QTcpSocket* socket, QString username);

private slots:
    void click_returnBtn();
    void click_sendBtn();
    void onReadyRead();
    void onFriendClicked(QListWidgetItem* item);
    void on_startButton_clicked();
    void on_setTargetButton_clicked();
    void handleFrame(const QVideoFrame &frame);
    void processDatagram();
    void receiveImageData(const QByteArray &datagram);
    void showAutoCloseMessageBox(const QString &message, int timeout, QWidget *parent);
    void handleAudioData();

private:
    void sendMediaData(const QByteArray& prefix, const QByteArray& data);
    Ui::Widget *ui;
    QTcpSocket* tcpSocket;
    QString myname;

    // 视频相关
    QCamera *camera;
    QMediaCaptureSession *captureSession;
    QVideoSink *videoSink;

    // 音频相关
    QAudioSource* audioInput = nullptr;
    QIODevice* audioIODevice = nullptr;
    bool isAudioActive = false;
    //音频本地播放相关
    QAudioSink *audioSink = nullptr;
    QIODevice *audioOutputDevice = nullptr;
    // 网络相关
    QUdpSocket *udpSocket;
    QHostAddress targetAddress = QHostAddress("47.96.159.221");
    quint16 targetPort = 7561;
    QString targetUser;
    bool isRegistered = false;
    quint16 randomPort;


};

#endif // WIDGET_H
